package com.glory.brepda.activity.recipe;

import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.EqpInfoEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.RecipeInfoEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class TERecipeDBWBActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvBDID)
    TextView tvBDID;
    @BindView(R.id.tvWrie)
    TextView tvWrie;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.tvEqpWorkType)
    TextView tvEqpWorkType;
    @BindView(R.id.tvProgramType)
    TextView tvProgramType;
    @BindView(R.id.tvRecipeID)
    TextView tvRecipeID;
    @BindView(R.id.tvRecipeEqpID)
    TextView tvRecipeEqpID;
    @BindView(R.id.tvRecipeVersion)
    TextView tvRecipeVersion;
    @BindView(R.id.tvRecipeType)
    TextView tvRecipeType;
    @BindView(R.id.tvDownLoad)
    TextView tvDownLoad;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.cbEQ)
    CheckBox cbEQ;
    @BindView(R.id.tvProductName)
    TextView tvProductName;
    @BindView(R.id.tvChipCode)
    TextView tvChipCode;
    @BindView(R.id.tvSpec)
    TextView tvSpec;
    @BindView(R.id.tvWaferSize)
    TextView tvWaferSize;
    @BindView(R.id.tvDieSize)
    TextView tvDieSize;
    @BindView(R.id.tvDieSingle)
    TextView tvDieSingle;
    @BindView(R.id.tvChipThickness)
    TextView tvChipThickness;
    private LotInfoEntity mLotInfoEntity = null;
    private RecipeInfoEntity mRecipeInfoEntity = null;
    private String mProcessName = "";
    private String mPlantName = "";
    private UpdateIPEntity mUpdateIPEntity = null;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_te_dbwb;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("查询");

        initEdit(etLot, s -> LotUtils.searchLotInfo(TERecipeDBWBActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                mLotInfoEntity = response;
                tvTitle.setText("TE下载Recipe" + "(" + response.getLOT().getSTEPDESC() + ")");
                tvBDID.setText(response.getBD_CODE());
                tvPackageType.setText(response.getPACKAGETYPE());
                tvWrie.setText(response.getWIRESPEC());
                tvWaferSize.setText(response.getWAFERSIZE());
                tvDieSize.setText(response.getDIESIZE());
                tvProductName.setText(response.getLOT().getPARTNAME());
                tvChipCode.setText(response.getCHIPNAME());
                tvSpec.setText(response.getPART().getSPEC2());
                tvDieSingle.setText(response.getISSINGLE());
                tvChipThickness.setText(response.getCHIPTHICKNESS());
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));

//        initEdit(etEqpWorkID, s -> LotUtils.searchEqpInfo(s, TERecipeDBWBActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
//            @Override
//            public void onSuccess(EqpInfoEntity response) {
//                tvEqpWorkType.setText(response.getEQUIPMENT().getEQPTYPE());
//                mEqpInfoEntity = response;
//            }
//
//            @Override
//            public void onFail(Throwable t) {
//                loadError(t,"EQPQUERY");
//            }
//        }));

        initEdit(etEqpWorkID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                            UpdateIPEntity body = response.body().getResponse().getBody();
                            mUpdateIPEntity = body;
                            tvEqpWorkType.setText(body.getEQUIPMENTTYPE());
                            tvProgramType.setText(body.getPROGRAMTYPE());
                            mProcessName = body.getPROCESSNAME();
                            mPlantName = body.getFAB();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "GETEQPINFOBYEQPID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EQUIPMENTID",s);
                NetUtils.requestPostNet("IF.GetEquipment","GetEquipment", TERecipeDBWBActivity.this,map,callBack);

            }
        });
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvDownLoad, R.id.tvBaseSubmit,R.id.cbEQ})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvDownLoad:
                if (mRecipeInfoEntity == null){
                    showToast("未获取到Recipe信息",false);
                    return;
                }

                if (getText(etEqpWorkID).equals("")){
                    showToast("设备ID不能为空",false);
                    return;
                }
                //上传、下载、切换
                JsonObject map = new JsonObject();
                map.addProperty("PlantName",mPlantName);
                map.addProperty("ProcessName",mProcessName);
                map.addProperty("EqpID",getText(etEqpWorkID));
                map.addProperty("RecipeEqpID",mRecipeInfoEntity.getRecipeEqpID());
                map.addProperty("RecipeID",mRecipeInfoEntity.getRecipeID());
                map.addProperty("EnableEQ",cbEQ.isChecked() ? 1 : 0);
                map.addProperty("RecipeVer",mRecipeInfoEntity.getRecipeVer());
                map.addProperty("LotID",getText(etLot));
                map.addProperty("id",mRecipeInfoEntity.getId());
//                if (cbEQ.isChecked()) {
                    FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                            ("正在上传程序",new TypeToken<BaseResponseBean<JSONObject>>() {
                            }.getType(), getSupportFragmentManager(), this) {

                        @Override
                        public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                            super.onSuccess(response);
//                            if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
//                                sendEqpRecipe(map,"CPC.SendEqpRecipe","SendEqpRecipe");
//                            }
                            showConfirmDialog(TERecipeDBWBActivity.this,"下载成功");

                        }

                        @Override
                        public void onError(Response<BaseResponseBean<JSONObject>> response) {
                            super.onError(response);
                            loadError(response.getException(),"UploadRecipe");

                        }
                    };
//                    NetUtils.requestPostNet("CPC.UploadRecipe","UploadRecipe",
//                            this,map,callBack);
//                }else {
                    sendEqpRecipe(map,"IF.SendEqpRecipe","SendEqpRecipe");
//                }
//                LotUtils.recipeDownLoad(this,getSupportFragmentManager(),mRecipeInfoEntity,
//                        getText(etEqpWorkID),getText(etLot),cbEQ.isChecked() ? 1 : 0);
                break;
            case R.id.tvBaseSubmit:
                if (mLotInfoEntity == null){
                    showToast("批次信息不能为空",false);
                    return;
                }

                if (mUpdateIPEntity == null){
                    showToast("暂无设备信息",false);
                    return;
                }

                LotUtils.searchRecipeInfo(this, getSupportFragmentManager(),tvChipThickness.getText().toString(), mLotInfoEntity,mUpdateIPEntity, getText(etEqpWorkID), new LotUtils.netCallback<RecipeInfoEntity>() {
                    @Override
                    public void onSuccess(RecipeInfoEntity response) {
                        tvRecipeEqpID.setText(response.getRecipeEqpID());
                        tvRecipeID.setText(response.getRecipeID());
                        tvRecipeType.setText(response.getRecipeType());
                        tvRecipeVersion.setText(response.getRecipeVer());
                        mRecipeInfoEntity = response;
                    }

                    @Override
                    public void onFail(Throwable t) {
                        loadError(t,"RECIPEQUERY");
                    }
                },mProcessName,tvEqpWorkType.getText().toString(),mPlantName);
                break;
        }
    }

    private void sendEqpRecipe(JsonObject map,String messageName,String message) {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (message.equals("SendEqpRecipe") ? "正在下载程序" : "正在切换程序",new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) { 
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
//                    if (message.equals("SendEqpRecipe")){
//                        sendEqpRecipe(map,"CPC.SwitchEqpRecipe","SwitchEqpRecipe");
//                    }else {
                        showConfirmDialog(TERecipeDBWBActivity.this,"下载成功");
//                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),message);

            }
        };
        NetUtils.requestPostNet(messageName,message, TERecipeDBWBActivity.this,map,callBack);
    }



}
